﻿using System;
using System.IO;
using System.Windows.Data;
using System.Windows.Media.Imaging;

namespace Rio.Framework.Pages.Helpers
{
    public class ExtractExtensionConverter : IValueConverter
    {
        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string extension = null;

            if (String.IsNullOrWhiteSpace(parameter as string))
            {
                extension = Path.GetExtension(Convert.ToString(value)).ToLowerInvariant();
            }
            else if (( parameter as string ).ToLowerInvariant() == "import")
            {
                extension = Path.GetExtension(Path.GetFileNameWithoutExtension(Convert.ToString(value))).ToLowerInvariant();
            }
            
            string iconFileName = String.Empty;

            switch (extension)
            {
                case ".jpg":
                case ".jpeg":
                    iconFileName = "document-jpg.png";
                    break;
                case ".gif":
                    iconFileName = "document-gif.png";
                    break;
                case ".bmp":
                    iconFileName = "document-bmp.png";
                    break;
                case ".png":
                    iconFileName = "document-png.png";
                    break;
                case ".doc":
                case ".docx":
                    iconFileName = "document-doc.png";
                    break;
                case ".xls":
                case ".xlsx":
                    iconFileName = "document-xls.png";
                    break;
                case ".ppt":
                case ".pptx":
                    iconFileName = "document-ppt.png";
                    break;
                case ".pdf":
                    iconFileName = "document-pdf.png";
                    break;
                case ".rtf":
                    iconFileName = "document-rtf.png";
                    break;
                case ".tiff":
                case ".tif":
                    iconFileName = "document-tif.png";
                    break;
                case ".txt":
                    iconFileName = "document-txt.png";
                    break;
                case ".zip":
                    iconFileName = "document-zip.png";
                    break;
                case ".msg":
                case "":
                    iconFileName = "document-msg.png";
                    break;
                default:
                    iconFileName = "document-unknow.png";
                    break;
            }

            return new BitmapImage(new Uri(String.Format("../../Assets/Images/Icons/{0}", iconFileName), UriKind.Relative));
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
